/*
 * @Author: TJN tjn@246@126.com
 * @Date: 2024-06-27 09:08:20
 * @LastEditors: TJN tjn@246@126.com
 * @LastEditTime: 2024-06-27 09:18:25
 * @FilePath: \hdcms-nest\src\upload\upload.decorator.ts
 * @Description:
 *
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved.
 */

import {
  UnsupportedMediaTypeException,
  UseInterceptors,
  applyDecorators,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
export function UploadDecorator(mime: string, file = 'file') {
  return applyDecorators(
    UseInterceptors(
      FileInterceptor(file, {
        storage: diskStorage({
          destination: 'uploads/',
          filename: (req, file, cb) => {
            cb(null, `${file.originalname}`);
          },
        }),
        fileFilter(req, file, callback) {
          if (!file.mimetype.includes(mime)) {
            callback(new UnsupportedMediaTypeException('文件类型错误'), false);
          } else {
            callback(null, true);
          }
        },
        limits: {
          fileSize: 1024 * 1024 * 5,
        },
      }),
    ),
  );
}
